<?php

/**
 * doseyeh actions.
 *
 * @package    Amman to London
 * @subpackage doseyeh
 * @author     Adel Abu-Saif <adeljas@msn.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class doseyehActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->forward('doseyeh', 'show');
    }

    public function executeShow($request) {
        $dos_id = $request->getParameter('id', false);
        $uid = $this->getUser()->getGuardUser()->getId();
        if ($dos_id === false || !is_numeric($dos_id))
            exit;



        $dos_course_id = DosTable::getInstance()
                ->createQuery("d")
                ->select("d.course_id")
                ->where("d.id=?", $dos_id)
                ->groupBy("d.course_id")
                ->fetchArray();

        $user_course = UserDosTable::getInstance()
                ->createQuery("ud")
                ->where("ud.user_id=?", $uid)
                ->andWhere("ud.course_id=?", $dos_course_id[0]['course_id'])
                ->fetchArray();

        if (!count($user_course))
            $this->forward404('Access denied');
            
            if (!$this->getUser()->hasAttribute($dos_id . $uid)) {
                $view_obj = new UserView();
                $view_obj->set('user_id', $uid);
                $view_obj->set('dos_id', $dos_id);
                try {
                    $view_obj->save();
                    $this->getUser()->setAttribute($dos_id . $uid, true);
                } catch (Exception $e) {
                    
                }
            }

        $views = UserViewTable::getInstance()->createQuery()
                ->select('COUNT(dos_id) as count')
                ->groupBy('user_id')
                ->where('dos_id = ?', $dos_id)
                ->fetchArray();


        $views_count = 0;
        foreach ($views as $one) {
            $views_count+=$one['count'];
        }

        $this->viewed_by_users = count($views);
        $this->views_count = $views_count;


        $this->obj = DosTable::getInstance()->find($dos_id);
        // $iid = $this->obj->getCourseFaculty()->getInstructor()->getId();
//        $this->icount = Doctrine_Query::create()
//                ->from('Dos d')
//                ->leftJoin('d.Course c')
//             //   ->where('c.instructor_id = ?', $iid)
//              //  ->andWhere('d.id <> ?', $this->obj->getId())
//                ->count();
        //   print_r($this->icount); die();
    }

    public function executeUpload_material($request) {
        $this->form = new UploadedMaterialForm();

        if ($request->isMethod('POST')) {
            $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
            if ($this->form->isValid()) {
                $this->success = true;
                $this->form->save();
            }
        }
    }

}
